package com.qiwenshare.file.domain;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.file.io.QiwenFile;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Comment;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Data
@Table(name = "userfile", uniqueConstraints = {
        @UniqueConstraint(name = "fileindex", columnNames = {"userId", "filePath", "fileName", "extendName", "deleteFlag", "isDir"})
})
@Entity
@TableName("userfile")
@AllArgsConstructor
public class UserFile {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    //@TableId(type = IdType.AUTO)
    @TableId
    @Column(nullable = false, length = 20)
    private String userFileId;

    @Comment("用户id")
    //@Column(columnDefinition = "bigint(20)")
    private String userId;

    @Comment("文件id")
    @Column(length = 20)
    private String fileId;

    @Comment("文件名")
    @Column(length = 100)
    private String fileName;

    @Comment("文件路径")
    @Column(length = 500)
    private String filePath;

    @Comment("扩展名")
    @ColumnDefault("''")
    @Column(length = 100)
    private String extendName;

    @Comment("是否是目录(0-否,1-是)")
    //@Column(columnDefinition="int(1)")
    private Integer isDir;

    @Comment("修改时间")
    @Column(length = 25)
    private String uploadTime;

    @Comment("删除标识(0-未删除，1-已删除)")
    //@Column(columnDefinition="int(11)")
    private Integer deleteFlag;

    @Comment("删除时间")
    @Column(length = 25)
    private String deleteTime;

    @Comment("删除批次号")
    @Column(length = 50)
    private String deleteBatchNum;

    @Comment("创建时间")
    @Column(length = 30)
    private String createTime;

    @Comment("创建用户id")
    @Column(length = 20)
    private String createUserId;

    @Comment("修改时间")
    @Column(length = 30)
    private String modifyTime;

    @Comment("修改用户id")
    @Column(length = 20)
    private String modifyUserId;

    public UserFile() {};
    public UserFile(QiwenFile qiwenFile, String userId, String fileId) {
        this.userFileId = IdUtil.getSnowflakeNextIdStr();
        this.userId = userId;
        this.fileId = fileId;
        this.filePath = qiwenFile.getParent();
        this.fileName = qiwenFile.getNameNotExtend();
        this.extendName = qiwenFile.getExtendName();
        this.isDir = qiwenFile.isDirectory() ? 1 : 0;
        String currentTime = DateUtil.getCurrentTime();
        this.setUploadTime(currentTime);
        this.setCreateUserId(userId);
        this.setCreateTime(currentTime);
        this.deleteFlag = 0;
    }

    public boolean isDirectory() {
        return this.isDir == 1;
    }

    public boolean isFile() {
        return this.isDir == 0;
    }

}
